package cn.hanyx.vlog.storage.controller;


import cn.hanyx.vlog.api.dto.ResponseObject;
import cn.hanyx.vlog.api.vo.Order;
import cn.hanyx.vlog.api.vo.Storage;
import cn.hanyx.vlog.storage.service.IStorageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hanyx
 * @since 2021-11-27
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private IStorageService storageService;

    @PutMapping
    public ResponseObject update(@RequestBody Storage storage) {
        return new ResponseObject(storageService.updateById(storage));
    }

    @PutMapping("/order")
    public ResponseObject updateByOrder(@RequestBody Order order) {
        QueryWrapper<Storage> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_code", order.getGoodsCode());
        Storage storage = storageService.getOne(wrapper);
        // 库存
        Integer quantity = storage.getQuantity() - order.getQuantity();
        storage.setQuantity(quantity);
        return new ResponseObject(storageService.updateById(storage));
    }
}

